package com.lzl.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.lzl.dto.TokenResult;

import javax.xml.bind.SchemaOutputResolver;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * //TODO
 *
 * @author zhaolinlin
 * @description //TODO
 * @createTime 2022/8/3 22:01
 */
public class JWTUtils {
    //盐
    private static final String  SIGN = "LZL%$@@";

    private static final String JWT_KEY_PHONE = "passengerPhone";

    private static final String JWT_KEY_IDENTITY = "identity";

    private static final String TOKEN_TYPE = "tokenType";

    private static final String TOKEN_TIME = "tokenTime";

    //token
    public static String generatorToken(String passengerPhone,String identity,String tokenType){
        HashMap<String, String> map = new HashMap<>();
        map.put(JWT_KEY_PHONE,passengerPhone);
        map.put(JWT_KEY_IDENTITY,identity);
        map.put(TOKEN_TYPE,tokenType);
//        token过期时间
        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.DATE,1);
        Date date = calendar.getTime();
        map.put(TOKEN_TIME,date.toString());

        JWTCreator.Builder builder = JWT.create();
        //整合map
        map.forEach(
                (k,v) ->{
                    builder.withClaim(k,v);
                }
        );
        //整合过期时间
//        builder.withExpiresAt(date);

        //生成token
        String sign = builder.sign(Algorithm.HMAC256(SIGN));

        return sign;

    }
    //解析token
    public static TokenResult parseToken(String token){
        DecodedJWT verify = JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);
//        Claim claim = verify.getClaim(JWT_KEY);
        String phone = verify.getClaim(JWT_KEY_PHONE).asString();
        String identity = verify.getClaim(JWT_KEY_IDENTITY).asString();
//        String tokenType = verify.getClaim(TOKEN_TYPE).asString();

        TokenResult tokenResult = new TokenResult();
        tokenResult.setPhone(phone);
        tokenResult.setIdentity(identity);
        return tokenResult;
    }

    public static TokenResult checkToken(String token){
        TokenResult tokenResult = null;
        try{
            tokenResult = JWTUtils.parseToken(token);
        }catch (Exception e){

        }
        return tokenResult;
    }



    public static void main(String[] args) throws Exception {
//        String token = generatorToken("18218758270","2");
//        System.out.println(token);
//        TokenResult s = parseToken(token);
//        String identity = s.getIdentity();
//        String phone = s.getPhone();
//        System.out.println("identiy :" + identity + "," + "passengerPhone:" + phone);
        Class aClass = Class.forName("com.lzl.util.JWTUtils");
        System.out.println("-------");
        JWTUtils jwtUtils = new JWTUtils();
        Class<? extends JWTUtils> aClass1 = jwtUtils.getClass();
        System.out.println(aClass1);
        Class<JWTUtils> jwtUtilsClass = JWTUtils.class;
        System.out.println(jwtUtilsClass);
        System.out.println("=============");
        System.out.println("反射生成token");
        Method generatorToken = jwtUtilsClass.getDeclaredMethod("generatorToken", String.class, String.class, String.class);
        JWTUtils jwtUtils1 = jwtUtilsClass.newInstance();
        Object invoke = generatorToken.invoke(jwtUtils1,"18218758270","1","123213");
        System.out.println(invoke);
        System.out.println("反射解析token");
        Method parseToken = jwtUtilsClass.getDeclaredMethod("parseToken", String.class);

        TokenResult invoke1 =(TokenResult) parseToken.invoke(jwtUtils1, invoke);
        String phone = invoke1.getPhone();
        String identity = invoke1.getIdentity();
        System.out.println("phone:" + phone + "\n" + "identity:" + identity );


//        System.out.println(aClass1 == jwtUtilsClass);
    }






}
